package com.syh.jn.scm.domain.operation.entity.supplier;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.operation.repository.supplier.OperaSupplierAccountReadReadRepository;
import com.syh.jn.scm.domain.operation.repository.supplier.OperaSupplierReadRepository;
import com.syh.jn.scm.dto.operation.supplier.OperaSupplierAccountMto;
import com.syh.jn.scm.dto.operation.supplier.OperaSupplierAccountSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.UUID;

/**
 * 添加供应商账号(运营端)
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class OperaSupplierAccount extends AbstractSimpleAggregate<Long> {
  /**
   * id
   */
  private Long businessEntityId;
  /**
   * 登录账号
   */
  private String loginAccount;
  /**
   * 登录密码
   */
  private String loginPassword;
  /**
   * 使用情况
   */
  private Short enable;


  public OperaSupplierAccount(OperaSupplierAccountSto sto) {
    ExtAssert.notNull(sto, "账号,密码不能为空");
    ValidationUtils.validate(sto);

    OperaSupplierReadRepository readRepository = (OperaSupplierReadRepository) SpringContextUtils.getBean(OperaSupplierReadRepository.class);
    OperaSupplierAccountReadReadRepository accountReadRepository = (OperaSupplierAccountReadReadRepository) SpringContextUtils.getBean(OperaSupplierAccountReadReadRepository.class);

    //生成带code的账号
    OperaSupplier operaSupplier = readRepository.getOne(Wrapper.build().append(Restrictions.eq("id",sto.getBusinessEntityId())));
    StringBuffer supAccount = new StringBuffer(sto.getLoginAccount()).append("@").append(operaSupplier.getCode());
    sto.setLoginAccount(supAccount.toString());
    //检查账号是否已存在
    Long count = accountReadRepository.getCountByAccountName(sto.getLoginAccount());
    if (count.intValue() > 0) {
      throw new BusinessException("帐号已经存在,请更换帐号名重试");
    }

    this.uuid = UUID.randomUUID().toString();
    this.businessEntityId = sto.getBusinessEntityId();
    this.loginAccount = sto.getLoginAccount();
    this.loginPassword = sto.getLoginPassword();
    this.createTimeStamp = System.currentTimeMillis();
    this.enable = 1;

  }
  public void modify(OperaSupplierAccountMto mto){
    ExtAssert.notNull(mto, "账号,密码不能为空");
    ValidationUtils.validate(mto);

    OperaSupplierReadRepository readRepository = (OperaSupplierReadRepository) SpringContextUtils.getBean(OperaSupplierReadRepository.class);
    OperaSupplierAccountReadReadRepository accountReadRepository = (OperaSupplierAccountReadReadRepository) SpringContextUtils.getBean(OperaSupplierAccountReadReadRepository.class);

    OperaSupplier operaSupplier = readRepository.getOne(Wrapper.build().append(Restrictions.eq("id",mto.getBusinessEntityId())));
    //重置密码时获取数据库的账号需要预处理
    String suffix = "@"+operaSupplier.getCode();
    if(mto.getLoginAccount().indexOf(suffix) > 0 ){
      mto.setLoginAccount(mto.getLoginAccount().split(suffix)[0]);
    }
    //生成带code的账号
    StringBuffer supAccount = new StringBuffer(mto.getLoginAccount()).append("@").append(operaSupplier.getCode());
    mto.setLoginAccount(supAccount.toString());
    //检查账号是否已存在
    Long count = accountReadRepository.getCountByAccountName(mto.getLoginAccount(),mto.getId());
    if (count.intValue() > 0) {
      throw new BusinessException("帐号已经存在,请更换帐号名重试");
    }

    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

}
